﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;

namespace SalomonWebsite.UC
{
	public partial class Pagination : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		private static Label BuildLabel(string str)
		{
			Label l = new Label();
			l.Text = str;
			return l;
		}

		#region
		/// <summary>
		/// 处理分页连接
		/// </summary>
		/// <param name="countRow">总记录数</param>
		/// <param name="pageIndex">当前显示页</param>
		/// <param name="pageSize">每页显示数</param>
		public void ProcessPageToHtml(int pageSize, int pageIndex, string paraStr, int countRow, int countPage)
		{
			// 总页数(注意处理余数)
			// int countPage = (countRow / pageSize) + (countRow % pageSize > 0 ? 1 : 0);

			if (pageIndex < 1)
			{
				pageIndex = 1;
			}
			if (pageIndex > countPage)
			{
				pageIndex = countPage;
			}
			
			PaginationPanel.Controls.Add(BuildLabel("<input type='hidden' id='countPage' value='" + countPage + "' />"));
            if (pageSize * pageIndex > countRow)
            { PaginationPanel.Controls.Add(BuildLabel("共<ins>" + countRow + " / " + countRow + "</ins>条记录")); }
            else { PaginationPanel.Controls.Add(BuildLabel("共<ins>" + pageSize * pageIndex + " / " + countRow + "</ins>条记录")); }
			PaginationPanel.Controls.Add(BuildLabel("页数<ins>" + pageIndex + " / " + countPage + "</ins>"));
			PaginationPanel.Controls.Add(BuildLabel("转到 <input type='text' id='pageno' class='pageno' name='pageno' maxlength='4' value='" + pageIndex + "' /> 页 <input id='pagego' title='' type='button' value='GO' />"));

			if (countRow > 0)
			{
				// 首页
				if (pageIndex == 1 || countPage == 1)
				{
					PaginationPanel.Controls.Add(BuildLabel("首页"));
				}
				else
				{
					PaginationPanel.Controls.Add(BuildLabel("<a href=\"?Page=1" + paraStr + "\">首页</a>"));
				}
				// 上一页
				if (pageIndex == 1)
				{
					PaginationPanel.Controls.Add(BuildLabel("前一页"));
				}
				else
				{
					PaginationPanel.Controls.Add(BuildLabel("<a href=\"?Page=" + Convert.ToString((pageIndex - 1), CultureInfo.InvariantCulture) + paraStr + "\">前一页</a>"));
				}
				// 下一页
				if (pageIndex == countPage)
				{
					PaginationPanel.Controls.Add(BuildLabel("下一页"));
				}
				else
				{
					PaginationPanel.Controls.Add(BuildLabel("<a href=\"?Page=" + Convert.ToString((pageIndex + 1), CultureInfo.InvariantCulture) + paraStr + "\">下一页</a>"));
				}
				// 尾页
				if (countPage == 1 || countPage == pageIndex)
				{
					PaginationPanel.Controls.Add(BuildLabel("尾页"));
				}
				else
				{
					PaginationPanel.Controls.Add(BuildLabel("<a href=\"?Page=" + Convert.ToString(countPage, CultureInfo.InvariantCulture) + paraStr + "\">尾页</a>"));
				}
			}
		}
		#endregion
	}
}